import lookup from './lookup';

function tokens2dom(tokens, data) {

  let html = '';
  for (let i = 0, len = tokens.length; i < len; i++) {
    const token = tokens[i];

    // 按类型拼接字符串
    switch (token[0]) {
      case 'name':
        if (token[1] === '.') {
          html += data;
        } else {
          html += lookup(data, token[1]);
        }
        break;
      case '#':
        // 递归解决数组嵌套的情况
        for (const item of data[token[1]]) {
          html += tokens2dom(token[2], item);
        }
        break;
      default:
        html += token[1];
    }
  }

  return html;
}

export default tokens2dom;
